layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--角色管理
     */
    var SubCabinet = {
        tableId: "subCabinetTable",    //表格id
        condition: {
        	nickname: ""
        }
    };

    /**
     * 初始化表格的列
     */
    SubCabinet.initColumn = function () {
        return [[
        	{type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
        	{field: 'parentId',sort: true, title: '所属机柜'},
        	{field: 'subNum', sort: true, title: '编号'},
        	{field: 'bigOrSmall', sort: true, title: '类型'},
            {field: 'isUse', sort: true, title: '是否使用'},
        	{align: 'center', toolbar: '#tableBar', title: '操作', width: 150}
        ]];
    };

    /**
     * 点击查询按钮
     */
    SubCabinet.search = function () {
        var queryData = {};
        queryData['nickname'] = $("#nickname").val();
        table.reload(SubCabinet.tableId, {where: queryData, page:{curr: 1}});
    };
    
    /**
     * 初始化
     */
    SubCabinet.openAddSubCabinet = function (cabinetId) {
    	var cabinetName = Feng.getUrlParam("cabinetName");
        $.ajax({
			url : Feng.ctxPath + "/cabinet/api/getDeviceDetailByDeviceSerial",
			data : {
				"deviceSerial" : cabinetName
			},
			type : 'post',
			dataType : 'json',
			beforeSend: function (request) {
				layerIndex = layer.load(2, 
					{
						time: 30*1000
						,content: '正在初始化...'
						, success: function (layero) {
					        layero.find('.layui-layer-content').css({
					            'padding-left': '40px',
					            'width': '100px'
					        });
				    	}
					}
				);
			},
			success : function(data) {
				layer.close(layerIndex);
				Feng.success("初始化成功!");
				table.reload(SubCabinet.tableId);
			},
			error : function(data) {
				 Feng.error("初始化失败!" + data.responseJSON.message + "!");
			}
        });
    };
    
    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    SubCabinet.updateCabinetSub = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改机柜信息',
            area: ['400px', '510px'],
            content: Feng.ctxPath + '/cabinet/cabinetSub_edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(SubCabinet.tableId);
            }
        });
    };
    
    /**
     * 点击打开柜门
     *
     * @param data 点击按钮时候的行数据
     */
    SubCabinet.openCabinet = function (data) {
    	var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/cabinet/api/openCabinet", function () {
                table.reload(SubCabinet.tableId);
                Feng.success("打开成功!");
            }, function (data) {
                Feng.error("打开失败!" + data.responseJSON.message + "!");
            });
            ajax.set("soltKey", data.soltKey);
            ajax.set("deviceSerial", data.parentId);
            ajax.start();
        };
        Feng.confirm("是否打开" + data.subNum + "号柜子?", operation);
    };
    /**
     * 点击打开全部柜门
     *
     * @param data 点击按钮时候的行数据
     */
    SubCabinet.openAllCabinet = function (cabinetName) {
    	var operation = function () {
    		var ajax = new $ax(Feng.ctxPath + "/cabinet/api/openAllCabinet", function () {
    			table.reload(SubCabinet.tableId);
    			Feng.success("打开成功!");
    		}, function (data) {
    			Feng.error("打开失败!" + data.responseJSON.message + "!");
    		});
    		ajax.set("deviceSerial", cabinetName);
    		ajax.start();
    	};
    	Feng.confirm("是否打开全部柜子?", operation);
    };
    
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SubCabinet.tableId,
        url: Feng.ctxPath + '/cabinet/cabinetSubList?cabinetId='+Feng.getUrlParam("cabinetName"),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        limits: [80,100,120,150],
        cols: SubCabinet.initColumn()
    });

    $("#cabinetId").html(Feng.getUrlParam("cabinetId"));
    $("#cabinetName").html(Feng.getUrlParam("cabinetName"));
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SubCabinet.search();
    }); 
    // 添加按钮点击事件
    $('#btnAdd').click(function () {
    	var cabinetId = Feng.getUrlParam("cabinetId");
    	SubCabinet.openAddSubCabinet(cabinetId);
    });
    // 添加按钮点击事件
    $('#openAllCabinet').click(function () {
    	var cabinetName = Feng.getUrlParam("cabinetName");
    	SubCabinet.openAllCabinet(cabinetName);
    });
  
    // 工具条点击事件
    table.on('tool(' + SubCabinet.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'openCabinet') {
        	SubCabinet.openCabinet(data);
        }
        if (layEvent === 'updateCabinetSub') {
        	SubCabinet.updateCabinetSub(data);
        }
    });
});
